@using Microsoft.AspNetCore.Components;
@inherits DynamicComponentContainer
@inject NavigationManager navigationManager
@inject HttpClient Http
@inject IStringLocalizer<Global> L

<MudNavMenu Style="flex: 1">
    <MudNavLink Href="@navigationManager.ToAbsoluteUri(" ").AbsoluteUri" Match="NavLinkMatch.All" Icon="@Icons.Material.Filled.Home">
        Frontend
    </MudNavLink>

    <MudNavLink Href="@navigationManager.ToAbsoluteUri("admin").AbsoluteUri" Match="NavLinkMatch.All" Icon="@Icons.Material.Filled.BarChart">
        @L["Dashboard"]
    </MudNavLink>
    <MudNavGroup Title="@L["Settings"]">
        <MudNavLink Href="@navigationManager.ToAbsoluteUri("admin/settings").AbsoluteUri" Icon="@Icons.Material.Filled.SettingsApplications">
            @L["MainSettings"]
        </MudNavLink>
        <MudNavLink Href="@navigationManager.ToAbsoluteUri("admin/settings/email").AbsoluteUri" Icon="@Icons.Material.Filled.Email">
            @L["Email"]
        </MudNavLink>
    </MudNavGroup>
    <MudNavGroup Title="@L["Localization"]" Icon="@Icons.Material.Filled.Public">
        <MudNavLink Href="@navigationManager.ToAbsoluteUri("admin/translations").AbsoluteUri" Icon="@Icons.Material.Filled.Translate">
            @L["Translations"]
        </MudNavLink>
        <MudNavLink Href="@navigationManager.ToAbsoluteUri("admin/pluralizationrules").AbsoluteUri" Icon="@Icons.Material.Filled.Group">
            @L["Pluralization rules"]
        </MudNavLink>
    </MudNavGroup>
    <MudNavLink Href="@navigationManager.ToAbsoluteUri("admin/users").AbsoluteUri" Icon="@Icons.Material.Filled.SupervisorAccount">
        @L["Users"]
    </MudNavLink>
    <MudNavLink Href="@navigationManager.ToAbsoluteUri("admin/roles").AbsoluteUri" Icon="@Icons.Material.Filled.SupervisorAccount">
        @L["Roles"]
    </MudNavLink>
    @if (tenant.Id == BlazorBoilerplate.Constants.Settings.DefaultTenantId)
    {
        <MudNavLink Href="@navigationManager.ToAbsoluteUri("admin/multitenancy").AbsoluteUri" Icon="@Icons.Material.Filled.AccountBalance">
            @L["MultiTenancy"]
        </MudNavLink>
    }
    <MudNavGroup Title="@L["AppAdminNavMonitoring"]">
        <MudNavLink Href="@navigationManager.ToAbsoluteUri("admin/apilog").AbsoluteUri" Icon="@Icons.Material.Filled.ChangeHistory">
            @L["AppAdminNavApiAuditLog"]
        </MudNavLink>
        <MudNavLink Href="@navigationManager.ToAbsoluteUri("admin/dblog").AbsoluteUri" Icon="@Icons.Material.Filled.Notes">
            @L["AppAdminNavDBLoggingView"]
        </MudNavLink>
    </MudNavGroup>
    @foreach (var component in components)
    {
        @CreateDynamicComponent(component);
    }
</MudNavMenu>

@code {
    TenantDto tenant = new TenantDto();

    [CascadingParameter]
    Task<AuthenticationState> authenticationStateTask { get; set; }

    protected override async Task OnParametersSetAsync()
    {
        var user = (await authenticationStateTask).User;

        if (user.Identity.IsAuthenticated)
        {
            var apiResponse = await Http.GetNewtonsoftJsonAsync<ApiResponseDto<TenantDto>>("api/admin/tenant");
            if (apiResponse.StatusCode == Status200OK)
            {
                tenant = apiResponse.Result;
            }
        }
    }
}
